

from itsdangerous import  TimedJSONWebSignatureSerializer as Serializer, BadSignature

from mall import settings
from utils.constants import TOKEN_EXPIRE




def generic_access_token(openid):
    serializer = Serializer(secret_key=settings.SECRET_KEY, expires_in=TOKEN_EXPIRE)
    data = {"openid": openid}
    token = serializer.dumps(data)
    return token.decode()



def check_open_id(token):
    """
    解密   1.创建序列化器   2.解密   3.返回
    """
    serializer = Serializer(secret_key=settings.SECRET_KEY, expires_in=TOKEN_EXPIRE)
    try:
        # 解密之后是个字典,不需要decode()
        result = serializer.loads(token)
    except BadSignature:
        return None
    return result